package com.qf.service.impl;


import com.qf.config.RedisKeyConfig;
import com.qf.config.SystemConfig;
import com.qf.dao.UserDao;
import com.qf.dao.UserdetailDao;
import com.qf.dao.UserlogDao;
import com.qf.dao.UserloginDao;
import com.qf.dto.UserDto;
import com.qf.etype.UserLoginType;
import com.qf.pojo.User;
import com.qf.pojo.Userlog;
import com.qf.service.UserService;
import com.qf.third.JedisUtil;
import com.qf.util.DateUtil;
import com.qf.util.EncryptUtil;
import com.qf.util.JwtUtil;
import com.qf.util.StrUtil;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzy
 * @since 2020-11-28
 */
@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserlogDao userlogDao;
    @Autowired
    private UserloginDao userloginDao;
    @Autowired
    private UserdetailDao userdetailDao;

    //查询全部
    @Override
    public R findAll() {
        return R.ok(userDao.selectList());
    }

    @Override
    public R login(UserDto userDto) {
        //检验参数
        if (userDto != null) {
            if (StrUtil.checkNoEmpty(userDto.getPassword(),userDto.getPhone())) {
                User user = userDao.selectByPhone(userDto.getPhone());
                if (user != null) {
                    //校验账号是否有效
                    if (user.getFlag() == 1) {
                        //校验密码是否正确
                        if (user.getPassword().equals(EncryptUtil.aesenc(SystemConfig.PASS_KEY,userDto.getPassword()))) {
                            //校验当前账号有没有在线 唯一登陆
                            if (JedisUtil.getInstance().ishave(RedisKeyConfig.AUTH_PHONE+userDto.getPhone())) {
                                //存在 当前账号已在线  生成挤掉信息
                                //获取在线的令牌
                                String tk = JedisUtil.getInstance().getStr(RedisKeyConfig.AUTH_PHONE + userDto.getPhone());
                                //存储挤掉信息
                                JedisUtil.getInstance().addHash(RedisKeyConfig.AUTH_OUT,tk,"你的账号在" + DateUtil.getTime()+ "在其他地点登录");
                                //原来的令牌 失效
                                JedisUtil.getInstance().delKeys(RedisKeyConfig.AUTH_TOKEN+tk);
                            }
                            //生成令牌 用户id
                            String token = JwtUtil.createToken(user.getId() + "");
                            //记录在线的手机号，用于校验唯一登录
                            JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_PHONE+user.getPhone(),token,RedisKeyConfig.AUTH_TIME);
                            //记录登录成功地令牌和对应用户的信息
                            JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_TOKEN+token,user.getId()+"",RedisKeyConfig.AUTH_TIME);
                            return R.ok(token);
                        }
                    }
                }
            }
        }

        return R.fail("您的账号或密码不正确，请检验后重新登录");
    }

    @Override
    @Transactional//开启事务
    public R register(UserDto userDto) {
        //校验参数
        if (userDto != null) {
            if (StrUtil.checkNoEmpty(userDto.getPhone(),userDto.getPassword())) {
                //数据库不存在该手机号才可以注册
                if (userDao.selectByPhone(userDto.getPhone()) == null) {
                    //密码转换为密文同时构建用户对象
                    User user = new User(userDto.getPhone(), EncryptUtil.aesenc(SystemConfig.PASS_KEY, userDto.getPassword()));
                    //校验新增是否成功
                    if (userDao.insertGetId(user) > 0) {
                        //初始化用户详情数据并且记录
                        userdetailDao.insertInit(user.getId());
                        userlogDao.add(new Userlog(user.getId(), UserLoginType.注册.getVal(),"新用户注册，手机号：" + userDto.getPhone()));
                        return R.ok();
                    }
                }
            }
        }


        return R.fail("注册失败，请检查网络后重试");
    }

    @Override
    public R checkPhone(String phone) {
        if (StrUtil.checkNoEmpty(phone)) {
            User user = userDao.selectByPhone(phone);
            if (user == null) {
                return R.ok();
            }
        }
        return R.fail("该手机号已注册，请直接登陆或找回密码");
    }

    @Override
    public R findPass(UserDto userDto) {
        if(userDto != null && StrUtil.checkNoEmpty(userDto.getPhone())) {
            userDto.setPassword(EncryptUtil.aesenc(SystemConfig.PASS_KEY,userDto.getPassword()));

            if (userDao.update(userDto) > 0) {
                return R.ok();
            }
        }
        return R.fail("找回密码失败，请联系客服");
    }
}
